package com.thunbu.thunbu_meeting_system.view.treeView;

import android.content.Context;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.impl.FullScreenPopupView;
import com.thunbu.lib_common.function.Consumer;
import com.thunbu.thunbu_meeting_system.R;
import com.thunbu.thunbu_meeting_system.bean.DepartmentModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;

public class TreeViewPopupView extends FullScreenPopupView {
    private RecyclerView rv;

    public TreeViewPopupView(@NonNull Context context) {
        super(context);
    }

    private Consumer<TreeNode> listener;
//    private TreeNode chooseNode;
    private List<TreeNode> nodes;

    public void setData(List<TreeNode> nodes,Consumer<TreeNode> listener){
        this.listener = listener;
//        this.chooseNode = chooseNode;
        this.nodes = nodes;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_treeview;
    }

    @Override
    protected void onCreate() {
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
//        addExpandIds();
//        checkExpand(nodes);
        TreeViewAdapter adapter = new TreeViewAdapter(nodes, Arrays.asList( new NodeBinder()));
        rv.setAdapter(adapter);
        adapter.setOnTreeNodeListener(new TreeViewAdapter.OnTreeNodeListener() {
            @Override
            public boolean onClick(TreeNode node, RecyclerView.ViewHolder holder) {
                if (!node.isLeaf()) {
                    //Update and toggle the node.
                    onToggle(!node.isExpand(), holder);
                }else {
                    listener.accept(node);
                }
                return false;
            }

            @Override
            public void onToggle(boolean isExpand, RecyclerView.ViewHolder holder) {
                NodeBinder.ViewHolder dirViewHolder = (NodeBinder.ViewHolder) holder;
                final ImageView ivArrow = dirViewHolder.getIvArrow();
                int rotateDegree = isExpand ? 90 : -90;
                ivArrow.animate().rotationBy(rotateDegree)
                        .start();
            }
        });
    }
    
    private List<Long> expandIds = new ArrayList<>();

//    private void addExpandIds() {
//        //没开始或者选中根节点就不用展开了
//        if(chooseNode == null || chooseNode.isRoot()) return;
//        TreeNode parent = chooseNode.getParent();
//        expandIds.add(parent.getContent().getmId());
//        if(parent.isRoot()){
//
//        }else {
//            chooseNode = parent;
//            addExpandIds();
//        }
//
//    }
//
//    private void checkExpand(List<TreeNode> nodes){
//        if(CollectionUtil.isEmpty(expandIds)) return;
//        for (TreeNode node : nodes) {
//            if(expandIds.contains(node.getContent().getmId())){
//                node.expand();
//                checkExpand(node.getChildList());
//            }else {
////                checkExpand(node.getChildList());
//            }
//        }
//    }
    

}
